package com.yanxi.controller;


import com.yanxi.anno.Log;
import com.yanxi.pojo.Dept;
import com.yanxi.pojo.Result;
import com.yanxi.service.DeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequestMapping("/depts")
@RestController
public class DeptController {

    public static final Logger log = LoggerFactory.getLogger(DeptController.class);
    @Autowired
    private DeptService deptService;

    //@RequestMapping(value = "/depts",method = RequestMethod.GET)
    @GetMapping
    public Result list() {
        //System.out.println("查询所有部门数据");
        log.info("查询所有部门数据");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }

    //删除
/*    @DeleteMapping("/depts")
    public Result deleteById(HttpServletRequest request) {
        String strId = request.getParameter("id");
        int id = Integer.parseInt(strId);
        System.out.println("删除id为" + id + "的部门");
        return Result.success();
    }*/
/*    @DeleteMapping("/depts")
    public Result deleteById(@RequestParam("id") Integer id) {
        System.out.println("删除id为" + id + "的部门");
        return Result.success();
    }*/

    @Log
    @DeleteMapping
    public Result deleteById(Integer id) {
        //System.out.println("删除id为" + id + "的部门");
        log.info("删除id为{} 的部门",id);
        deptService.deleteById(id);
        return Result.success();
    }

    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept) {
        //System.out.println("添加部门"+dept);
        log.info("添加部门:{}",dept);
        deptService.add(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {
        //System.out.println("查询id为" + id + "的部门");
        log.info("查询id为{}的部门",id);
        Dept dept = deptService.getInfo(id);
        return Result.success(dept);
    }

    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept) {
        //System.out.println("修改部门"+dept);
        log.info("修改部门:{}",dept);
        deptService.update(dept);
        return Result.success();
    }
}
